package com.project.servlet;
import com.github.pagehelper.PageInfo;
import com.project.bean.ManBean;
import com.project.service.IManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Controller
public class FindAllServlet extends HttpServlet {
    @Autowired
    private IManService service;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码集
        req.setCharacterEncoding("utf-8");
        //设置响应信息的MIME类型的编码集--设置响应
        resp.setContentType("text/html;charset=utf-8");
        //得到页码
        String pg = req.getParameter("pageNO");
        int pageNo=1;
        if (pg!=null){
            pageNo=Integer.parseInt(pg);
        }
        //调入业务方法，得到结果
        PageInfo<ManBean>pageInfo = service.findAll(pageNo);
        //得到当前页需要显示的记录
        List<ManBean>list =  pageInfo.getList();
        //总页数
        int pages = pageInfo.getPages();

        String str = "<table border='1'  width='60%' cellspacing='0' ";
        str+="<thead><tr><th>姓名</th><th>生日</th><th>操作</th></tr></thead>";
        str +="<tbody>";
        for(ManBean man : list){
            str += "<tr><td>"+man.getName()+"</td><td>"+man.getBirthday()+
                    "</td><td><a style='margin-left:10px;' href='del?id="+man.getId()+"'>删除</a>" +
                    "<a style='margin-left:10px;' href='findById?id="+man.getId()+"'>修改</a></td></tr>";
        }
        str+="</tbody></table>";

        for (int i = 1;i<= pages;i++){
            str += "<a style='margin-left:10px;' href='findAll?pageNO="+i+"'>"+i+"</a>";
        }
        resp.getWriter().println(str);
    }
}